Imports Microsoft.VisualBasic
Imports System.Data

Public Class Busca

    Private _SearchFor As String
    Private _Result As System.Data.DataTable

    Public Property SearchFor() As String
        Get
            Return _SearchFor
        End Get
        Set(ByVal value As String)
            _SearchFor = value
        End Set
    End Property

    Public Property Result() As System.Data.DataTable
        Get
            Return _Result
        End Get
        Set(ByVal value As System.Data.DataTable)
            _Result = value
        End Set
    End Property

    Public Function Search(ByVal SearchFor As String) As System.Data.DataTable

        Dim oSqlConnection As SqlClient.SqlConnection
        Dim oSqlCommand As SqlClient.SqlCommand
        Dim oSqlDataAdapter As SqlClient.SqlDataAdapter
        Dim QuerySQL As String
        Dim oSQLParameterNome As SqlClient.SqlParameter
        Dim oSQLParameterDescricao As SqlClient.SqlParameter

        Try

            oSqlConnection = New SqlClient.SqlConnection
            oSqlCommand = New SqlClient.SqlCommand
            oSqlDataAdapter = New SqlClient.SqlDataAdapter

            oSqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("DefaultConnectionString").ConnectionString

            oSQLParameterNome = New SqlClient.SqlParameter("Nome", SqlDbType.VarChar, 30)
            oSQLParameterNome.Value = String.Concat("%", SearchFor, "%")

            oSQLParameterDescricao = New SqlClient.SqlParameter("Descricao", SqlDbType.VarChar, 255)
            oSQLParameterDescricao.Value = String.Concat("%", SearchFor, "%")

            QuerySQL = "SELECT Nome, Descricao, '~/run.aspx?RegistroID=' + Cast(RegistroID as varchar) AS Arquivo FROM RegistroManutencao WHERE ((Nome LIKE @Nome) OR (Descricao LIKE @Descricao)) AND (Disponivel = 1)"


            With oSqlCommand
                .CommandText = QuerySQL
                .CommandType = CommandType.Text
                .Connection = oSqlConnection
                .Parameters.Add(oSQLParameterNome)
                .Parameters.Add(oSQLParameterDescricao)
            End With

            _Result = New DataTable

            oSqlDataAdapter.SelectCommand = oSqlCommand
            oSqlDataAdapter.Fill(_Result)

            Return _Result

        Catch ex As Exception

            _Result = Nothing
            Return _Result

        End Try

    End Function

End Class
